package executors;
import java.util.ArrayList;

import objects.Event;
import objects.Result;
import objects.SortedEventList;
import objects.Command;


public class SearchExecutor {
	public static Result execute(Command command, SortedEventList data, SortedEventList completeTasks) {

		ArrayList<Event> results = new ArrayList<Event>();
		ArrayList<String> query = command.getSearchKeyWords();
		boolean success = true; 



		try{ 
			for (int i = 0; i<data.size(); i++){
				String taskdetails = data.get(i).getDetails();
				for (int j = 0; j<query.size(); j++){
					if (!taskdetails.contains(query.get(j))){
						break; //the moment task details doesn't contain a word in the query, we go on to the next task.
					}
					if (j==query.size()-1){ //otherwise, this is the end of query, and it contains every word in the query; we thus add the task.
						success = results.add(data.get(i));
					}
				}
			}

			//                        for (int i = 0; i<completeTasks.size(); i++){
			//                                String taskdetails = completeTasks.get(i).getDetails();
			//                                for (int j = 0; j<query.size(); j++){
			//                                        if (!taskdetails.contains(query.get(j))){
			//                                                break;
			//                                        }
			//                                        if (j==query.size()-1){
			//                                                if(results.add(data.get(i))){
			//                                                        success = false;
			//                                                }
			//                                        }
			//                                }
			//                        }


			return new Result(success, results);

		}catch(Exception e){
			return new Result(false, results, null, e.toString());
		}
	}


}
